/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.its.dsw2.view;

import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import mx.its.dsw2.biblioteca.bl.CategoriaBlLocal;
import mx.its.dsw2.biblioteca.modelo.Categoria;

/**
 *
 * @author Juan Carlos
 */
@ManagedBean
@SessionScoped
public class CatergoryBean {

    //-------------------------EJB's--------------------------------------------
    @EJB
    private CategoriaBlLocal categoriaBl;
    //--------------------------------------------------------------------------
    //----------------------------VARIABLES-------------------------------------
    private List<Categoria> lista;
    private Categoria categoria;
    private String action ;
    //--------------------------------------------------------------------------

    //---------------------Constructores----------------------------------------
    public CatergoryBean() {
        categoria = new Categoria();
    }
    //--------------------------------------------------------------------------

    //----------------------GETTER and SETTER-----------------------------------
    public Categoria getCategoria() {
        return categoria;
    }

    public void setCategoria(Categoria categoria) {
        this.categoria = categoria;
    }

    public List<Categoria> getList() {
        lista = categoriaBl.listAll();
        return lista;
    }
    //--------------------------------------------------------------------------
    
    //----------------Acciones--------------------------------------------------
    public String procesarAccion(){
        if (action.equalsIgnoreCase("Nuevo")) {
        categoriaBl.register(categoria);            
        }else if(action.equalsIgnoreCase("Edit")){
        categoriaBl.modify(categoria);    
        }else if(action.equalsIgnoreCase("Delete")){
        categoriaBl.delete(categoria);
        }
        action = "";
        return "CategoryList";
    }
    
     public void prepararNuevo(ActionEvent evt){
        categoria= new Categoria();
        action = "Nuevo";
    }    
   
     public void prepararEditar(ActionEvent evt){
         action="Editar";
         int index=Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
         categoria=lista.get(index);     
     }
     
     public void prepararEliminar(ActionEvent evt){
         action="Eliminar";
         int index=Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
         categoria=lista.get(index);
     }     
     
     public String llamadaPaginas(){
         String link="";
         if(action.equalsIgnoreCase("Editar")||action.equalsIgnoreCase("Nuevo")){
             link="CategoryCreateEdit";
         }else{
             if(action.equalsIgnoreCase("Eliminar")){
                 link="CategoryList";
             }
             }
         
         return link;
     }     
     //--------------------------------------------------------------------------   
}
